﻿using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using KillBill.Presentation.Common;
using KillBill.Presentation.Modules.Administration.View;
using KillBill.Presentation.Modules.Products.View;
using KillBill.Presentation.Modules.Sales.View;
using KillBill.Presentation.Shell.AdministrationServiceReference;
using KillBill.Presentation.Shell.SecurityServiceReference;
using KillBill.Presentation.Shell.View;
using KillBill.Presentation.Modules.Suppliers.View;

namespace KillBill.Presentation.Shell
{
    public partial class Shell : Form
    {
        private int _childFormNumber = 0;
        private LogInView _logInView;
        private SecurityServiceClient _securityService;

        public Shell()
        {
            InitializeComponent();

            _logInView = new LogInView();
            _securityService = new SecurityServiceClient();
        }
        
        #region Windows Operations


        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        #endregion 
      
        #region Events

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Shell_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                LogOut();
            }
            finally
            {
                Application.Exit();
            }
        }

        private void Shell_Shown(object sender, EventArgs e)
        {
            ShowLogIn();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var about = new AboutView();
            about.ShowDialog();
        }

        private void conectarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowLogIn();
        }

        private void desconectarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LogOut();
        }

        private void facturaciónToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowBilling();
        }

        private void tsbCustomers_Click(object sender, EventArgs e)
        {
            ShowCustomers();
        }

        #endregion

        #region UI Operations 

        private void LoadUIOperations()
        {
            if (Context.Session != null && Context.Session.Employee != null)
            {
                DisableConnect();
                verToolStripMenuItem.Visible = true;
                tsMain.Visible = true;

                foreach (ToolStripMenuItem control in verToolStripMenuItem.DropDownItems)
                {
                    if (control.Tag != null)
                        control.Visible = control.Tag.ToString().Contains(Context.Session.Employee.EmployeeRoleID);
                }
                foreach (ToolStripButton control in tsMain.Items)
                {
                    if (control.Tag != null)
                        control.Visible = control.Tag.ToString().Contains(Context.Session.Employee.EmployeeRoleID);
                }
                LoadDefaultOperationByUser();
            }
            else
            {
                EnableConnect();
                verToolStripMenuItem.Visible = false;
                tsMain.Visible = false;
            }
        }

        private void LoadDefaultOperationByUser()
        {
            Form f;
            if (Context.Session != null && Context.Session.Employee != null)
                switch (Context.Session.Employee.EmployeeRoleID)
                {
                    case "Administrador":
                        ShowEmployees();
                        break;
                    case "Bodeguero":
                        ShowInventory();
                        break;
                    case "Cajero":
                        ShowBilling();
                        break;
                }
        }

        private void ShowEmployees()
        {
            var f = new EmployeeListView { MdiParent = this, WindowState = FormWindowState.Maximized };
            f.Show();
        }

        private void ShowLogIn()
        {
            _logInView.ShowDialog();
            LoadUIOperations();
        }
        
        private void EnableConnect()
        {
            conectarToolStripMenuItem.Visible = true;
            desconectarToolStripMenuItem.Visible = false;
        }

        private void DisableConnect()
        {
            conectarToolStripMenuItem.Visible = false;
            desconectarToolStripMenuItem.Visible = true;
        }

        private void LogOut()
        {
            if (Context.Session != null && Context.SessionID != null)
            {
                _securityService.LogOut(Context.SessionID);
                Context.Session = null;
                LoadUIOperations();
            }
        }

        private void ShowInventory()
        {
            var f = new ProductMainView();
            f.MdiParent = this;
            f.WindowState = FormWindowState.Maximized;
            f.Show();
        }

        private void ShowBilling()
        {
            var f = new BillingView();
            f.MdiParent = this;
            f.WindowState = FormWindowState.Maximized;
            f.Show();
        }

        private void ShowCustomers()
        {
            var f = new CustomerListView();
            f.MdiParent = this;
            f.WindowState = FormWindowState.Maximized;
            f.Show();
        }

        

        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            //var form = new ProviderAddEditView();
            //var form = new ProviderListView();
            //var form = new CustomerAddEditView();
            var form = new EmployeeAddEditView();
            form.MdiParent = this;
            form.Show();
        }

        private void clientesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowCustomers();
        }

        private void proveedoresToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowProviders();
        }

        private void ShowProviders()
        {
            var f = new ProviderListView();
            f.MdiParent = this;
            f.WindowState = FormWindowState.Maximized;
            f.Show();
        }

        private void productosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowProducts();
        }

        private void ShowProducts()
        {
            var f = new ProductMainView();
            f.MdiParent = this;
            f.WindowState = FormWindowState.Maximized;
            f.Show();
        }

        private void tsbInventary_Click(object sender, EventArgs e)
        {
            ShowProducts();
        }

        private void tsbProviders_Click(object sender, EventArgs e)
        {
            ShowProviders();
        }

        private void tsbEmployees_Click(object sender, EventArgs e)
        {
            ShowEmployees();
        }

        private void tsbBilling_Click(object sender, EventArgs e)
        {
            ShowBilling();
        }
    }
}
